import cn.hutool.core.date.StopWatch;
import edu.uci.ics.jung.algorithms.layout.*;
import edu.uci.ics.jung.graph.*;

import java.awt.*;

public class JungForceDirectedDemo {
    public static void main(String[] args) {
        // 创建图对象
        Graph<Integer, String> graph = new DirectedSparseGraph<>();

        StopWatch stopWatch = new StopWatch();
        // 添加节点
        for (int i = 0; i < 1000; i++) {
            graph.addVertex(i);
        }
        for (int i = 0; i < 100; i++) {
            // 添加边
            graph.addEdge("Edge" + i, i, i+1);
        }
//        // 添加边
//        graph.addEdge("Edge1", 1, 2);
//        graph.addEdge("Edge2", 2, 3);
//        graph.addEdge("Edge3", 3, 4);
//        graph.addEdge("Edge4", 4, 1);
        
        // 创建力导向布局对象
        FRLayout<Integer, String> layout = new FRLayout<>(graph);
        
        // 设置布局参数
        layout.setSize(new Dimension(300, 300));

        stopWatch.start();
        // 进行布局计算
        layout.initialize();
        for (int i = 0; i < 100; i++) {
            layout.step();
        }
        stopWatch.stop();
        
        // 获取节点的位置
        for (Integer vertex : graph.getVertices()) {
            System.out.println("Vertex " + vertex + ": " + layout.getX(vertex) + ", " + layout.getY(vertex));
        }
        System.out.println("cost: " + stopWatch.getTotalTimeMillis());
    }
}
